<template>
  <el-container class="home-container">
    <!--    头部区-->
    <el-header>
      <div>
        <img src="../assets/favicon.png" alt="">
        <span>渐悟电商管理系统</span>
      </div>
      <div>
        <el-button type="info" @click="logout">退出</el-button>
      </div>

    </el-header>
    <!--    页面主体-->
    <el-container>
      <!--      侧边栏-->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <el-menu
          router
          unique-opened
          :default-active="activePath"
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409EFF"
          :collapse="isCollapse"
          :collapse-transition="false">

          <!--一级菜单-->
          <el-submenu :index="item.id + ''" v-for="item in menulist" :key="item.id">
            <template slot="title">
              <i :class="menuIcon[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>

            <!--二级菜单-->
            <el-menu-item :index="'/'+subItem.path" v-for="subItem in item.children" :key="subItem.id"
                          @click="saveNavState('/'+subItem.path)">
              <i class="el-icon-menu"></i>
              <span>{{subItem.authName}}</span>
            </el-menu-item>
          </el-submenu>

        </el-menu>
      </el-aside>
      <!--      main内容-->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
  export default {
    data () {
      return {
        menulist: [],
        menuIcon: {
          101: 'el-icon-s-shop',
          102: 'el-icon-s-claim',
          103: 'el-icon-s-flag',
          125: 'el-icon-user-solid',
          145: 'el-icon-s-data'
        },
        isCollapse: false, // 侧边栏
        activePath: '' // 激活链接
      }
    },
    created () {
      this.getMuneList()
      this.activePath = window.localStorage.getItem('activePath')
    },
    methods: {
      logout () {
        window.localStorage.clear()
        this.$router.push('/')
      },
      // 获取菜单
      async getMuneList () {
        const { data: res } = await this.$http.get('menus')
        if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
        this.menulist = res.data
      },
      // 侧边栏 展开 隐藏
      toggleCollapse () {
        this.isCollapse = !this.isCollapse
      },
      // 保存当前操作路径
      saveNavState (activePath) {
        this.activePath = activePath
        window.localStorage.setItem('activePath', activePath)
      }
    }
  }
</script>

<style lang="less" scoped>
  .home-container {
    height: 100%;
  }

  .el-header {
    background: #373d41;
    display: flex;
    justify-content: space-between;
    align-items: center;
    color: #fff;
    font-size: 20px;

    img {
      width: 8%;
    }

    > div {
      display: flex;
      align-items: center;

      span {
        margin-left: 15px;
      }
    }
  }

  .el-aside {
    background: #333744;

    .el-menu {
      border-right: none;
    }
  }

  .el-main {
    background: #eaedf2;
  }

  .toggle-button {
    background-color: #4A5064;
    color: #fff;
    font-size: 10px;
    line-height: 24px;
    text-align: center;
    letter-spacing: 0.2rem;
    cursor: pointer;
  }
</style>
